package socketTest;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zyc
 * @create 2022--05--06 10:09
 */
public class MyService {

    public void start(int port) {
        ServerSocket server = null;
        try {
            server = new ServerSocket(port);
            Socket socket;
            while ((socket = server.accept()) != null) {
                System.out.println("客户端已连接");
                try {
                    ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    Object accObj = objectInputStream.readObject();
                    System.out.println("收到请求：" + accObj.toString());
                    String sendObj = "收到[" + accObj + "]请求!!!";
                    objectOutputStream.writeObject(sendObj);
                    objectOutputStream.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyService myService = new MyService();
        myService.start(5555);
    }
}
